package com.king.spark.rdd.transform

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Author wdl
 * @Date 2022/11/22 16:34
 */
object Spark_RDD_map {

  def main(args: Array[String]): Unit = {

    // 创建Spark运行配置对象
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("WordCount")

    // 创建Spark上下文环境对象（连接对象）t
    val sc: SparkContext = new SparkContext(sparkConf)

    val dataRDD: RDD[Int] = sc.makeRDD(List(1, 2, 3, 4), 2)

    val value: RDD[Int] = dataRDD.map(_ * 2)

    val value1: RDD[Int] = dataRDD.mapPartitions(datas => {
      List(datas.max).iterator
    })

    dataRDD.mapPartitionsWithIndex((index: Int, datas: Iterator[Int]) => {
      index match {
        case 1 => datas
        case _ => Nil.iterator
      }
    }).collect().foreach(println)


//    value1.foreach(println)
//    value.foreach(println)

    sc.stop()
  }
}
